<?php

namespace App\Http\Controllers\Admin\Cashier;

use App\Http\Requests\StoreApply;
use App\Http\Requests\UpdateApply;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Apply;

class ApplyController extends Controller
{

    public function __construct()
    {
        view()->share([
            '_cashier' => 'active open',
            '_apply' => 'active',
        ]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }
        };
        $appliers = Apply::where($where)->orderBy('sort_order', 'desc')->paginate(env('pageSize'));
//        return $appliers;
        return view('admin.cashier.apply.index', compact('appliers'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.cashier.apply.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreApply $request)
    {
        Apply::create($request->all());
        return redirect(route('cashier.apply.index'))->with('success', '新增供应商成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $apply = Apply::find($id);
        return view('admin.cashier.apply.edit', compact('apply'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateApply $request, $id)
    {
        $apply = Apply::find($id);
        $apply->update($request->all());
        return redirect(route('cashier.apply.index'))->with('success', '编辑供货商成功~');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Apply::destroy($id);
        return back()->with('success', '删除供货商成功~');
    }

    /** ajax排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $brand = Apply::find($request->id);
        $brand->sort_order = $request->sort_order;
        $brand->save();
    }


    /** 删除多条
     * @param Request $request
     */
    public function destroy_more(Request $request)
    {
        $checked_id = $request->checked_id;
//        return $checked_id;

//        foreach ($checked_id as $id) {
        Apply::destroy($checked_id);
//        }

    }
}
